<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//用户个人中心
class UserController extends CommonController{
	public function index(){
		$_SESSION['history_url'] = $_SERVER['REQUEST_URI'];
		if(IS_POST){
			$userModel = new \Home\Model\User;
			if(!$userModel->money()) View::error($userModel->getError());
			go(U('index'));
		}
//		1、调订单表数据
		$ordersModel = new \Home\Model\Orders;
		$ordersData = $ordersModel->get();
//		3、统计订单的数量
		$ordernum = count($ordersData);
		View::with('ordernum',$ordernum);
		$n = Q('get.n',0,'intval');
		$status = 0;
		if($n == 1){
			$status = $n;
			$ordersData = $ordersModel->where("status='未支付'")->get();
		}else if($n == 2){
			$status = $n;
			$ordersData = $ordersModel->where("status='已完成'")->get();
		}else if($n == 3){
			$status = $n;
			$ordersData = $ordersModel->where("status='已付款'")->get();
		}else if($n == 4){
			$status = $n;
			$ordersData = $ordersModel->where("status='已发货'")->get();
		}else if($n == 5){
			$status = $n;
			$ordersData = $ordersModel->where("status='已取消'")->get();
		}
//		分配变量
		View::with('status',$status);
//		sp($status);
//		sp($ordersData);
//		将订单列表的数据组合到订单表中
		foreach($ordersData as $k => $v){
			$ordersData[$k]['lists'] = Db::table("orders_lists")->join("goods","goods_gid","=","gid")->where("order_oid={$v['oid']}")->get();
		}
//		sp($ordersData);
//		2、查询到数据，分配
		View::with('ordersData',$ordersData);
//		3、查询用户表，得到余额
		$uid = $_SESSION['app_uid'];
		$yue = Db::table("user_app")->where("uid={$uid}")->pluck("balance");
		View::with('yue',$yue);
		View::make();
	}
	public function address(){
//		sp($_SESSION);
		$_SESSION['history_url'] = $_SERVER['REQUEST_URI'];
		if(IS_POST){
			$addressModel = new \Home\Model\address;
			if(!$addressModel->Userstore()) View::error($addressModel->getError());
		}
		$uid = $_SESSION['app_uid'];
//		根据登陆的用户id，查询地址
		$addModel = new \Home\Model\address;
		$addressData = $addModel->where("user_app_uid={$uid}")->get();
//		分配变量
		View::with('addressData',$addressData);
		$ordersModel = new \Home\Model\Orders;
		$ordersData = $ordersModel->get();
//		sp($ordersData);
//		3、统计订单的数量
		$ordernum = count($ordersData);
		View::with('ordernum',$ordernum);
		View::make();
	}
//	订单支付，ajax
	public function getOrderAjax(){
		if(IS_POST){
//			2、减余额
			$oid = Q('post.oid',0,'intval');
//			（1）根据订单id查询价格
			$price = Db::table("orders")->where("oid={$oid}")->pluck("totalprice");
//			（2）获得该账户账户余额
			$uid = Q('post.uid',0,'intval');
			$yue = Db::table("user_app")->where("uid={$uid}")->pluck("balance");
			$data = array(
				'balance' => $yue - $price
			);
			$userModel = new \Home\Model\User;
			$userModel->where("uid={$uid}")->save($data);
			$orderModel = new \Home\Model\Orders;
			$data = array(
				'status'=>'已付款'
			);
			$orderModel->where("oid={$oid}")->save($data);
			go(U('index'));
		}
	}
//	删除订单
	public function delOrders(){
//		接收，要删除那个订单
		$oid = Q('get.oid',0,'intval');
////		实例化 订单表模型
		$ordersModel = new \Home\Model\Orders;
////		先删除订单表
//		$ordersModel->delete($oid);
//		$orderslistsmodel = new \Home\Model\Orderslists;
////		再删除与其对应的订单列表
//		$olids = $orderslistsmodel->where("order_oid={$oid}")->lists("olid");
//		foreach($olids as $v){
//			$orderslistsmodel->delete($v);
//		}
//		2、改成，只改变状态
		$ordersModel->where("oid={$oid}")->update(array('status'=>'已取消'));
		go(U('index'));
	}
//	订单详情
	public function OrderDetail(){
//		1、调订单表数据
		$ordersModel = new \Home\Model\Orders;
		$ordersData = $ordersModel->get();
//		2、统计订单的数量
		$ordernum = count($ordersData);
		View::with('ordernum',$ordernum);
//		3、接收到uid，查询地址数据
		$oid = Q('get.oid',0,'intval');
//		sp($oid);
		$addData = Db::table("orders")->where("oid={$oid}")->first();
//		sp($addData);
		View::with('addData',$addData);
//		4、查询订单列表
		$OrderListsData = Db::table("orders_lists")->join("goods","goods_gid","=","gid")->where("order_oid={$oid}")->get();
		View::with('OrderListsData',$OrderListsData);
//		sp($OrderListsData);
		View::make();
	}
//	删除地址
	public function del(){
		$adid = Q('get.adid',0,'intval');
		$model = new \Home\Model\address;
		$model->delete($adid);
		go(U('address'));
	}
//	设置默认值
	public function defaultAd(){
		$uid = Q('get.uid',0,'intval');
		$adid = Q('get.adid',0,'intval');
		$model = new \Home\Model\address;
		if(!$model->defaultAd($adid,$uid)) View::error($model->getError());
		go(U('address'));
	}
//	个人资料
	public function users(){
		View::make();
	}
//	修改成已完成状态
	public function complete(){
		$oid = Q('get.oid',0,'intval');
		$ordersModel = new \Home\Model\Orders;
		$ordersModel->where("oid={$oid}")->update(array('status'=>'已完成'));
		go(U('index'));
	}
}




 ?>